import { apiRequest, oauthRequest } from '@/utils/HttpRequest';

/** 获取当前的用户 GET /api/currentUser */
export async function fetchDataMenuList(options?: { [key: string]: any }) {
  return oauthRequest.get<API.MenuTreeList[]>('menu/list');
}

export async function findByDataMenuTreeList(options?: { [key: string]: any }) {
  return oauthRequest.getList<API.MenuList[]>('menu/listTree');
}
export async function findByDataMenuTreeRoleList(options?: {
  [key: string]: any;
}) {
  return new Promise<API.MenuTreeList[]>((resolve, reject) => {
    let catceList = localStorage.getItem('catceList') || null;
    if (catceList) {
      resolve(JSON.parse(catceList));
    } else {
      oauthRequest
        .getList<API.MenuList[]>('menu/listTree')
        .then((res) => {
          let result: API.MenuTreeList[] = [];
          localStorage.setItem('catceList', JSON.stringify(res));
          resolve(result);
        })
        .catch((err) => {
          reject(err);
        });
    }
  });
}
export async function findByDataMenuListSelect() {
  return oauthRequest.get<API.BaseArrayDataResult<API.SelectItem>>(
    'menu/listSelect',
  );
}
export function navList() {
  return oauthRequest.get<any>('menu/navList');
}
export function navByList() {
  return oauthRequest.get<any>('menu/list');
}
export function navEdit(params: API.MenuTreeInfo) {
  return oauthRequest.post<any>('menu/edit', params);
}

export async function editMenu(params: API.Category) {
  return oauthRequest.post<API.BaseResult>('menu/edit', params);
}

/** 获取当前的用户 GET /api/currentUser */
export async function fetchDataCategoryList(options?: { [key: string]: any }) {
  return apiRequest.get<API.MenuTreeList>('syscategory/list');
}

export async function findByDataCategoryTreeList(params?: any) {
  return apiRequest.getList<API.MenuList[]>('syscategory/listTree', params);
}

export async function findByDataCategoryListSelect() {
  return apiRequest.get<API.SelectItem[]>('syscategory/listSelect');
}

export async function editCategory(params: API.Category) {
  return apiRequest.post<API.BaseResult>('syscategory/edit', params);
}

export async function findByDataCategoryListPageSelect() {
  return apiRequest.get<API.SelectItem[]>('syscategory/listPageSelect');
}
export async function editCategoryByDelete(id: any) {
  return apiRequest.post<any>('syscategory/byDelete?id=' + id, { id: id });
}
